Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

198
Vistas
How do I format am-pm-of-day to be "AM/PM" rather than "a.m./p.m."?

DateTimeFormatter's API reference seems to miss details for formatting am-pm-of-day properly, I could always use String.replace() but I feel like it makes more sense to have an option change a.m. to AM and/or p.m. to PM using "aaa...". Is there a way to do this?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Localized per CLDR

When formatting the textual representation of a date-time object, decisions about capitalization and abbreviation are matters left to the rules of localization. Those rules are defined as part of a Locale object. In the latest versions of Java built on the codebase of the OpenJDK project, the Locale class gets its localization data from the Common Locale Data Repository (CLDR) maintained by the Unicode Consortium.

So no, there is no way to specify use of “AM” versus “a.m.” other than to specify one Locale object versus another.

ZonedDateTime zdt = ZonedDateTime.now( ZoneId.of( "Asia/Tokyo" ) ) ;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedTime( FormatStyle.MEDIUM ) ;
String outputCaFr = zdt.format( f.withLocale( Locale.CANADA_FRENCH ) ) ;
String outputUkEn = zdt.format( f.withLocale( Locale.UK) ) ;

If you insist on a particular format, you’ll have to do the string manipulation yourself. But I suggest letting Locale, DateTimeFormatter, and the CLDR do their work of localizing rather than hard-coding trivial formatting tweaks.

Note that the CLDR, and therefore the Locale & DateTimeFormatter classes, support sub-cultures. Explore the CLDR’s definitions if the higher-level culture does not meet your expectations. One of the sub-cultures may hold the choice of localization rules you want.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda